package com.xiaolanshu.controller;

import com.xiaolanshu.entity.Order;
import com.xiaolanshu.service.GoodsService;
import com.xiaolanshu.service.OrderService;
import com.xiaolanshu.vo.TableDataVo;
import com.xiaolanshu.vo.TableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 收益管理报表
     * @param param
     * @return
     */
    @GetMapping("/earnings")
    public ResponseEntity earnings(@RequestBody Map param){

        //根据日期查询订单
        List<Order> orderList = orderService.findOrder(param);

        //遍历订单列表获取每个商品订单的总成本，总金额，总返利金额
        //所有商品订单总成本
        Double costs = 0D;
        //所有商品订单总金额；
        Double amounts = 0D;
        //所有商品订单总返利金额
        Double returnAmounts = 0D;

        for (Order order:orderList
             ) {

            //根据订单商品id查询商品成本
            Double cost = goodsService.findCost(order.getGoodsId());
            //将商品成本*商品总数
            DecimalFormat df=new DecimalFormat("0.00");
            costs +=Double.parseDouble( df.format(cost * order.getCount()));

            amounts += order.getAmount();
            returnAmounts += order.getReturnAmount();

        }

        //计算净收入 = 总金额 - 总成本 - 总返利金额
        Double income = amounts - costs - returnAmounts;

        Map<String,Double> map = new HashMap<>();
        map.put("总金额",amounts);
        map.put("总成本",costs);
        map.put("总返利金额",returnAmounts);
        map.put("净收入",income);

        return ResponseEntity.ok(map);

    }

    /**
     * 返利管理报表
     * @param param
     * @return
     */
    @GetMapping("/rebate")
    public  ResponseEntity rebate(@RequestBody Map param){

        String year = (String) param.get("year");

        TableVo tableVo = orderService.ReturnChart(year);

        return ResponseEntity.ok(tableVo);
    }

}
